package com.xiaoshuidi.cloud.module.member.service.point;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.common.pojo.PageParam;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.util.MyBatisUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.point.vo.MemberPointChangesExportVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.point.vo.MemberPointChangesPageReqVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.point.vo.MemberPointChangesPageRespVo;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.MallPointRecord;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.PointsForm;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.TenantPointsRecordDto;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.TenantPointsRecordVo;
import com.xiaoshuidi.cloud.module.member.convert.point.PointsConvert;
import com.xiaoshuidi.cloud.module.member.enums.point.PointRecordEnums;
import com.xiaoshuidi.cloud.module.member.enums.point.PointSendTypeEnum;
import com.xiaoshuidi.cloud.module.member.mapper.point.MemberPointsRecordMapper;
import com.xiaoshuidi.cloud.module.member.pojo.point.MemberPointsRecordEntity;
import com.xiaoshuidi.cloud.module.member.pojo.user.MemberUser;
import com.xiaoshuidi.cloud.module.member.service.user.MemberUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author neil
 * @version generator
 * @description 租客积分记录service层
 * @date 2022-12-14 18:08:07
 */
@Service
public class MemberPointsRecordService {

    @Autowired
    MemberPointsRecordMapper tenantPointsRecordMapper;
    @Autowired
    MemberUserService memberUserService;


    /**
     * 查询本年度生日积分新增记录
     *
     * @param tenantUserId
     * @return
     * @author neil
     * @date 2022-12-28 10:22:45
     */
    public TenantPointsRecordDto getBirthDayPointsRecord(Long tenantUserId) {
        return tenantPointsRecordMapper.getBirthDayPointsRecord(tenantUserId);
    }

    /**
     * 根据recordNo查询积分记录
     *
     * @param recordNo
     * @return
     * @author neil
     * @date 2023-1-10 10:22:45
     */
    public TenantPointsRecordDto getPointRecordByRecordNo(String recordNo) {
        return tenantPointsRecordMapper.getPointRecordByRecordNo(recordNo);
    }

    /**
     * 查询参数设置
     *
     * @param params
     * @author neil
     * @date 2023-1-5 15:27:45
     */
    private LambdaQueryWrapper<MemberPointsRecordEntity> getWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<MemberPointsRecordEntity> wrapper = new LambdaQueryWrapper<>();
        if (null != params.get("tenantUserId")) {
            String tenantUserId = String.valueOf(params.get("tenantUserId"));
            wrapper.eq(StringUtils.isNotBlank(tenantUserId), MemberPointsRecordEntity::getTenantUserId, tenantUserId);
        }
        //recordType
        if (null != params.get("recordType")) {
            String recordType = String.valueOf(params.get("recordType"));
            wrapper.eq(StringUtils.isNotBlank(recordType), MemberPointsRecordEntity::getRecordType, recordType);
        }
        //增减
        if (null != params.get("changeValueType")) {
            String changeValueType = String.valueOf(params.get("changeValueType"));
            wrapper.eq(StringUtils.isNotBlank(changeValueType), MemberPointsRecordEntity::getChangeValueType, changeValueType);
        }
        //手动自动
        if (null != params.get("changeType")) {
            String changeType = String.valueOf(params.get("changeType"));
            wrapper.eq(StringUtils.isNotBlank(changeType), MemberPointsRecordEntity::getChangeType, changeType);
        }
        //更新开始时间
        if (null != params.get("queryBeginTime")) {
            String queryBeginTime = (String) params.get("queryBeginTime");
            wrapper.ge(StringUtils.isNotBlank(queryBeginTime), MemberPointsRecordEntity::getCreateTime, queryBeginTime);
        }
        //更新结束时间
        if (null != params.get("queryEndTime")) {
            String queryEndTime = (String) params.get("queryEndTime");
            wrapper.le(StringUtils.isNotBlank(queryEndTime), MemberPointsRecordEntity::getCreateTime, queryEndTime);
        }
        //手机号
        if (null != params.get("tenantUserIds")) {
            List<Long> tenantUserIds = (List) params.get("tenantUserIds");
            if (tenantUserIds.size() > 0) {
                wrapper.in(true, MemberPointsRecordEntity::getTenantUserId, tenantUserIds);
            }
        }
        return wrapper;
    }

    public List<TenantPointsRecordDto> list(Map<String, Object> params) throws Exception {
        List<MemberPointsRecordEntity> entity = tenantPointsRecordMapper.selectList(getWrapper(params));
        if (entity == null) {
            throw new Exception("无数据 ");
        }
        return PointsConvert.INSTANCE.convertRecords(entity);
    }

    public void insert(MemberPointsRecordEntity entityDto) {
        tenantPointsRecordMapper.insert(entityDto);
    }

    public PageResult<MemberPointChangesPageRespVo> getPointsChangePage(MemberPointChangesPageReqVo reqVo) {
        if (StringUtils.isNotBlank(reqVo.getPhone())) {
            //手机号查询租客
            MemberUser user = memberUserService.getUserByMobile(reqVo.getPhone());
            if (user != null) {
                reqVo.setTenantUserId(user.getId());
            } else {
                return new PageResult<>();
            }
        }
        IPage<MemberPointChangesPageReqVo> page = MyBatisUtils.buildPage(reqVo);
        List<MemberPointChangesPageRespVo> list = tenantPointsRecordMapper.selectPointsChangePage(page, reqVo);
        PageResult<MemberPointChangesPageRespVo> pageResult = new PageResult(list, page.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    public List<MemberPointChangesExportVo> exportPointsChangeList(MemberPointChangesPageReqVo reqVo) {
        if (StringUtils.isNotBlank(reqVo.getPhone())) {
            //手机号查询租客
            MemberUser user = memberUserService.getUserByMobile(reqVo.getPhone());
            if (user != null) {
                reqVo.setTenantUserId(user.getId());
            } else {
                return new ArrayList<>();
            }
        }
        List<MemberPointChangesPageRespVo> list = tenantPointsRecordMapper.selectPointsChangePage(reqVo);
        List<MemberPointChangesExportVo> temp = PointsConvert.INSTANCE.convertExport(list);
        return temp;
    }

    public PageResult<TenantPointsRecordVo> getTenantPointsRecords(PointsForm form) {
        IPage<TenantPointsRecordVo> page = MyBatisUtils.buildPage(form);
        List<TenantPointsRecordVo> list = tenantPointsRecordMapper.selectTenantPointsRecords(page, form);
        for (TenantPointsRecordVo vo : list) {
            vo.setSendType(PointSendTypeEnum.getNameByCode(vo.getChangeType().intValue()));
            vo.setRecordTypeStr(PointRecordEnums.getPointRecordEnumsByValue(vo.getRecordType()).getMsg());
        }
        PageResult<TenantPointsRecordVo> pageResult = new PageResult<>(list, page.getTotal());
        return pageResult;
    }

    public PageResult<MallPointRecord> getMallPointsChangeDetail(Long id, Integer pageNo, Integer pageSize) {
        if (pageSize > 1000) {
            pageSize = 1000;
        }
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(pageNo);
        pageParam.setPageSize(pageSize);
        IPage<MallPointRecord> page = MyBatisUtils.buildPage(pageParam);
        List<MallPointRecord> list= tenantPointsRecordMapper.getMallPointsChangeDetail(page,id);
        PageResult<MallPointRecord> pageResult = new PageResult(list, page.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    public List<MemberPointsRecordEntity> getBirthdayRecordByCreateTime(LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<MemberPointsRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberPointsRecordEntity::getRecordType, PointRecordEnums.BIRTHDAY.getValue());
        wrapper.between(MemberPointsRecordEntity::getCreateTime, startTime, endTime);
        return tenantPointsRecordMapper.selectList(wrapper);
    }
}

